
package com.rentalHome.config;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Created by LeeXyan on 2017/03/03.
 */

@WebFilter(filterName = "loginFiler", urlPatterns = {"/","*.html"})
public class LoginFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        String msg = (String)request.getSession().getAttribute("msg");
        String url = request.getServletPath();
        System.out.println("已经进来了！");

       if("/".equals(url) && !"SUCCESS".equals(msg)){
            response.sendRedirect("/list.html");
            return;
        }
        if(!"/list.html".equals(url) && !"SUCCESS".equals(msg)){
            response.sendRedirect("/index.html");
            return;
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

